﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SFP.Animation2D;

namespace SFP
{
    public class GameObject : LayerObject
    {
        public static HashSet<GameObject> GetHashSetByType(LayerManager layerManager, string type)
        {
            HashSet<GameObject> gameObjects = new HashSet<GameObject>();

            foreach (Layer layer in layerManager.getLayers())
            {
                gameObjects.UnionWith(GetHashSetByType(layer, type));
            }

            return gameObjects;
        }

        public static HashSet<GameObject> GetHashSetByType(Layer layer, string type)
        {
            HashSet<GameObject> gameObjects = new HashSet<GameObject>();

            foreach (LayerObject layerObj in layer.getLayerObjects())
            {
                if (layerObj is GameObject)
                {
                    GameObject gameObj = (GameObject)layerObj;

                    if (gameObj.Type.Equals(type))
                        gameObjects.Add(gameObj);
                }
            }

            return gameObjects;
        }

        public ImageSequencer ImageSequencer { get; set; }
        public bool UseImageSequence { get; set; }
        public Bitmap Image { get; set; }
        public string ImagePath { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public int GroupColumns {get; set; }
        public int GroupRows { get; set; }
        public int Width {
            get
            {
                if (Image != null)
                    return GroupColumns * Image.Width;
                else
                    return 0;
            }
        }
        public int Height
        {
            get
            {
                if (Image != null)
                    return GroupRows * Image.Height;
                else
                    return 0;
            }
        }

        public GameObject()
        {
            Name = "";
            Type = "";
            GroupColumns = 1;
            GroupRows = 1;
        }

        public Rectangle GetRectangle()
        {
            return new Rectangle((int)Math.Round(X, 0, MidpointRounding.AwayFromZero),
                                 (int)Math.Round(Y, 0, MidpointRounding.AwayFromZero),
                                 Width, Height);
        }

        public override void draw(Graphics g)
        {
            if (UseImageSequence && ImageSequencer != null)
            {
                for (int column = 0; column < GroupColumns; column++)
                {
                    for (int row = 0; row < GroupRows; row++)
                    {
                        int animX = (int)Math.Round(X + column * Image.Width, 0, MidpointRounding.AwayFromZero);
                        int animY = (int)Math.Round(Y - row * Image.Height, 0, MidpointRounding.AwayFromZero);
                        ImageSequencer.Position = new Point(animX, animY);
                        ImageSequencer.draw(g);
                    }
                }
            }
            else if (!UseImageSequence && Image != null)
            {
                for (int column = 0; column < GroupColumns; column++)
                {
                    for (int row = 0; row < GroupRows; row++)
                    {
                        int drawX = (int)Math.Round(X + column * Image.Width, 0, MidpointRounding.AwayFromZero);
                        int drawY = (int)Math.Round(Y - row * Image.Height - Image.Height, 0, MidpointRounding.AwayFromZero);
                        g.DrawImage(Image, drawX, drawY);
                    }
                }
            }
            else
            {
                int drawX = (int)Math.Round(X, 0, MidpointRounding.AwayFromZero);
                int drawY = (int)Math.Round(Y - 50, 0, MidpointRounding.AwayFromZero);
                g.FillRectangle(Brushes.White, new Rectangle(drawX, drawY, 50, 50));
                g.DrawLine(Pens.Red, drawX, drawY, drawX + 50, drawY + 50);
                g.DrawLine(Pens.Red, drawX + 50, drawY, drawX, drawY + 50);
            }
        }
    }
}
